package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.CoordinateEntity;
import com.hualude.roadsign.repository.CoordinateRepository;
import com.hualude.roadsign.repository.GuardrailFaultRepository;
import com.hualude.roadsign.repository.GuardrailRepository;
import com.hualude.roadsign.service.GuardrailManageService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/8/18.
 */
@Service
public class GuardrailManageServiceImpl implements GuardrailManageService {
    @Resource
    private GuardrailRepository guardrailRepository;
    @Resource
    private CoordinateRepository coordinateRepository;
    @Resource
    private GuardrailFaultRepository guardrailFaultRepository;

    @Override
    public DTResponseDto getGuardrail4DT(DTRequestDto dtRequestDto,
                                         String actorIds,
                                         Integer orgId,
                                         Boolean audit,
                                         String startRoad,
                                         String endRoad,
                                         String roadName) throws Exception {
        List<GuardrailDto> guardrailDtos = guardrailRepository.getGuardrail4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        Integer count = guardrailRepository.getGuardrail4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, guardrailDtos);
    }

    @Override
    @Transactional
    public Integer deleteGuardrail(String guardrailIds) throws Exception {
        String[] guardrailIdArray = guardrailIds.split(",");
        for (String guardrailId : guardrailIdArray) {
            guardrailFaultRepository.deleteGuardrailFault(Integer.parseInt(guardrailId));
            coordinateRepository.deleteCoordinateByTypeId(Integer.parseInt(guardrailId), 0);
            guardrailRepository.deleteGuardrail(Integer.parseInt(guardrailId));
        }
        return guardrailIdArray.length;
    }

    @Override
    @Transactional
    public Integer auditGuardrail(String guardrailIds) throws Exception {
        String[] guardrailIdArray = guardrailIds.split(",");
        for (String guardrailId : guardrailIdArray) {
            guardrailRepository.auditGuardrail(Integer.parseInt(guardrailId));
        }
        return guardrailIdArray.length;
    }

    @Override
    public List<String> getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        return guardrailRepository.getStartRoadByRoadName(roadName, actorIds, orgId);
    }

    @Override
    public List<String> getEndRoadByStartRoad(String guardrailRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        return guardrailRepository.getEndRoadByStartRoad(guardrailRoadName, startRoad, actorIds, orgId);
    }

    @Override
    @Transactional
    public Integer addGuardrail(GuardrailDto guardrailDto) throws Exception {

        Integer guardrailId = guardrailRepository.save(guardrailDto.toBean()).getGuardrailId();
        String points = guardrailDto.getGuardrailPoints();
        String[] pointArray = points.split(";");
        for (int i = 0; i < pointArray.length; i++) {
            String[] point = pointArray[i].split(",");
            CoordinateEntity coordinateEntity = new CoordinateEntity();
            coordinateEntity.setCoordinateTypeId(guardrailId);
            coordinateEntity.setCoordinateLongitude(FormatNumber.resize(point[0]));
            coordinateEntity.setCoordinateLatitude(FormatNumber.resize(point[1]));
            coordinateEntity.setCoordinateOrder(i);
            coordinateEntity.setCoordinateType(0);
            coordinateRepository.save(coordinateEntity).getCoordinateId();
        }
        return guardrailId;
    }

    @Override
    @Transactional
    public Integer updateGuardrail(GuardrailDto guardrailDto) throws Exception {
        if (guardrailDto.getGuardrailPoints().equals("no") == false) {
            coordinateRepository.deleteCoordinateByTypeId(guardrailDto.getGuardrailId(), 0);
            String points = guardrailDto.getGuardrailPoints();
            String[] pointArray = points.split(";");
            for (int i = 0; i < pointArray.length; i++) {
                String[] point = pointArray[i].split(",");
                CoordinateEntity coordinateEntity = new CoordinateEntity();
                coordinateEntity.setCoordinateTypeId(guardrailDto.getGuardrailId());
                coordinateEntity.setCoordinateLongitude(point[0]);
                coordinateEntity.setCoordinateLatitude(point[1]);
                coordinateEntity.setCoordinateOrder(i);
                coordinateEntity.setCoordinateType(0);
                coordinateRepository.save(coordinateEntity).getCoordinateId();
            }
        }
        return guardrailRepository.updateGuardrail(
                guardrailDto.getGuardrailId(),
                guardrailDto.getProjectNumber(),
                guardrailDto.getGuardrailProvince(),
                guardrailDto.getGuardrailCity(),
                guardrailDto.getGuardrailArea(),
                guardrailDto.getGuardrailRoadName(),
                guardrailDto.getGuardrailStartRoad(),
                guardrailDto.getGuardrailEndRoad(),
                guardrailDto.getGuardrailType(),
                guardrailDto.getGuardrailMaterial(),
                guardrailDto.getGuardrailPhoto(),
                guardrailDto.getGuardrailInstallTime(),
                guardrailDto.getGuardrailInstallOrg(),
                guardrailDto.getGuardrailPeriod(),
                guardrailDto.getGuardrailServiceLife(),
                guardrailDto.getGuardrailRemark(),
                guardrailDto.getGuardrailCreatorId(),
                guardrailDto.getGuardrailModifierId(),
                guardrailDto.getGuardrailOrgId(),
                guardrailDto.getGuardrailMainOrgId(),
                guardrailDto.getGuardrailColor(),
                guardrailDto.getGuardrailMileage()
        );
    }

    @Override
    public List<CoordinateDto> getCoordinatesById(Integer roadLineId, Integer type) throws Exception {
        return coordinateRepository.getCoordinatesById(roadLineId, type);
    }

    @Override
    public List<String> getCity4Guardrail(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return guardrailRepository.getCity4Guardrail(actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByCity(String city, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return guardrailRepository.getStartRoadByCity(city, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getSectionByStartRoad4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return guardrailRepository.getSectionByStartRoad4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<GuardrailDto> getGuardrail4APP(String actorIds, Integer orgId, Integer mCount, Integer mSize, String city, String startRoad, String endRoad, String roadName) throws Exception {
        return guardrailRepository.getGuardrail4APP(actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName);
    }

    @Override
    public GuardrailDto getGuardrailById(Integer guardrailId) throws Exception {
        List<CoordinateDto> coordinateDtos = coordinateRepository.getCoordinatesById(guardrailId, 0);
        GuardrailDto guardrailDto = guardrailRepository.getGuardrailById(guardrailId);
        guardrailDto.setLatitude(coordinateDtos.get(0).getCoordinateLatitude());
        guardrailDto.setLongitude(coordinateDtos.get(0).getCoordinateLongitude());
        return guardrailDto;
    }

    @Override
    public List<String> getRoadName4GuardrailByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception {
        return guardrailRepository.getRoadName4GuardrailByCity(actorIds, orgId, city, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return guardrailRepository.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getEndRoadByStartRoad4App(String city, String roadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return guardrailRepository.getEndRoadByStartRoad4App(city,roadName , startRoad, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<GuardrailDto> getGuardrail4Map4App(Integer orgId,
                                                   String actorIds,
                                                   double neLongitude,
                                                   double neLatitude,
                                                   double swLongitude,
                                                   double swLatitude) throws Exception {
        List<GuardrailDto> guardrailDtos = guardrailRepository.getGuardrail4Map4App
                (orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
        for(GuardrailDto guardrailDto : guardrailDtos) {
            List<CoordinateDto> coordinateDtos = coordinateRepository.getCoordinatesById(guardrailDto.getGuardrailId(), 0);
            guardrailDto.setCoordinateDtos(coordinateDtos);
        }
        return guardrailDtos;
    }

}
